package controllers

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"purchase-mall/models"
	"purchase-mall/services"
)

func AliYunHandler(c *gin.Context) {
	var param models.AliParam
	err := c.ShouldBindJSON(&param)
	//fmt.Printf("aliyun param is  %v", param)
	if err != nil {
		zap.L().Error("Post Ali.kafka.common params failed", zap.Error(err))
		// 判断err是不是validator.ValidationErrors 类型
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			ResponseError(c, CodeInvalidParam)
			return
		}
		ResponseErrorWithMsg(c, CodeInvalidParam, removeTopStruct(errs.Translate(trans)))
		return
	}

	list := make([]*models.Aliyun, 0, len(param.MetricName))
	//服务层处理kafka
	for metric, maxValue := range param.MetricName {
		//fmt.Printf("aliyun res is  %v,%v\n", metric, maxValue)
		err, res := services.AliMetric(&param, metric, maxValue.(string))
		if err != nil {
			zap.L().Error("services.KafkaMetric() failed", zap.Error(err))
			ResponseError(c, CodeServerBusy)
			return
		}

		temp := &models.Aliyun{
			PeakValue:  res.PeakValue,
			PeakTime:   res.PeakTime,
			Period:     res.Period,
			TypeName:   res.TypeName,
			MetricName: res.MetricName,
			MaxValue:   res.MaxValue,
			CreatedAt:  res.CreatedAt,
			StatDate:   res.StatDate,
			Instance:   res.Instance,
			Host:       res.Host,
		}
		list = append(list, temp)

		//fmt.Printf("aliyun res is  %v", res)

	}
	ResponseSuccess(c, list)
	//
	//err, res := services.KafkaMetric(&param, "Maximum")
	//
	//fmt.Printf("aliyun res is  %v", res)
	//
	//ResponseSuccess(c, res)
}

func LogstotalHandler(c *gin.Context) {
	var param models.AliParam
	err := c.ShouldBindJSON(&param)
	//fmt.Printf("aliyun param is  %v", param)
	if err != nil {
		zap.L().Error("Post Ali.kafka.common params failed", zap.Error(err))
		// 判断err是不是validator.ValidationErrors 类型
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			ResponseError(c, CodeInvalidParam)
			return
		}
		ResponseErrorWithMsg(c, CodeInvalidParam, removeTopStruct(errs.Translate(trans)))
		return
	}

	list := make([]*models.Aliyun, 0, len(param.MetricName))
	//服务层处理kafka
	for metric, maxValue := range param.MetricName {
		//fmt.Printf("aliyun res is  %v,%v\n", metric, maxValue)
		err, res := services.LogsMetric(&param, metric, maxValue.(string))
		if err != nil {
			zap.L().Error("services.KafkaMetric() failed", zap.Error(err))
			ResponseError(c, CodeServerBusy)
			return
		}

		temp := &models.Aliyun{
			PeakValue:  res.PeakValue,
			PeakTime:   res.PeakTime,
			Period:     res.Period,
			TypeName:   res.TypeName,
			MetricName: res.MetricName,
			MaxValue:   res.MaxValue,
			CreatedAt:  res.CreatedAt,
			StatDate:   res.StatDate,
			Instance:   res.Instance,
			Host:       res.Host,
		}
		list = append(list, temp)

		//fmt.Printf("aliyun res is  %v", res)

	}
	ResponseSuccess(c, list)

}

//
//func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *cms20190101.Client, _err error) {
//	config := &openapi.Config{
//		// 您的 AccessKey ID
//		AccessKeyId: accessKeyId,
//		// 您的 AccessKey Secret
//		AccessKeySecret: accessKeySecret,
//	}
//	// 访问的域名
//	config.Endpoint = tea.String("metrics.cn-beijing.aliyuncs.com")
//	_result = &cms20190101.Client{}
//	_result, _err = cms20190101.NewClient(config)
//
//	fmt.Printf("aliyun res is  %v", _result)
//	// 入库 pg
//	//if err := pg.InsertData(); err != nil {
//	//	zap.L().Error("Kafka.InsertData failed", zap.Error(err))
//	//	return
//	//}
//
//	//服务层处理
//	if err := services.InsertData(); err != nil {
//		zap.L().Error("Kafka.InsertData failed", zap.Error(err))
//		return
//	}
//
//	return _result, _err
//}
//
//func _main() (_err error, response *cms20190101.DescribeMetricListResponse) {
//	client, _err := CreateClient(tea.String("LTAI5tJbjwHniMeGGvL1bPTf"), tea.String("xf0A7cgFxdPiELy0YDbMML6UXBPYE8"))
//	if _err != nil {
//		return _err, nil
//	}
//
//	describeMetricListRequest := &cms20190101.DescribeMetricListRequest{
//		Namespace:  tea.String("acs_kafka"),
//		MetricName: tea.String("instance_disk_capacity"),
//		Period:     tea.String("60"),
//		StartTime:  tea.String("2022-10-27 00:00:00"),
//		EndTime:    tea.String("2022-10-28 00:00:00"),
//		Dimensions: tea.String("[{\"instanceId\": \"alikafka_post-cn-7pp2tik4j014\"}]"),
//	}
//	runtime := &util.RuntimeOptions{}
//	tryErr := func() (_e error) {
//		defer func() {
//			if r := tea.Recover(recover()); r != nil {
//				_e = r
//			}
//		}()
//		// 复制代码运行请自行打印 API 的返回值
//		response, _err = client.DescribeMetricListWithOptions(describeMetricListRequest, runtime)
//		if _err != nil {
//			return _err
//		}
//
//		return nil
//	}()
//
//	if tryErr != nil {
//		var error = &tea.SDKError{}
//		if _t, ok := tryErr.(*tea.SDKError); ok {
//			error = _t
//		} else {
//			error.Message = tea.String(tryErr.Error())
//		}
//		// 如有需要，请打印 error
//		_, _err = util.AssertAsString(error.Message)
//		if _err != nil {
//			return _err, nil
//		}
//
//		return error, nil
//	}
//	return _err, response
//}
